/**
 * Description: 管理员管理
 *
 * @author      Eden
 * @datetime    2016/11/28 16:37
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {
    
    "use strict";

    var api = {
        getRolesUrl: "/admin/role/roles",
        saveRoleUrl: "/admin/role/save",
        editRoleUrl: "/admin/role/edit",
        getByIDUrl: "/admin/role/getItem"
    };



    sm.controller('admin-admin-role-add', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.roles = [];
        $scope.pagetype = 'add';
        $scope.roleName = '';
        $scope.saveRoles = [];

        $scope.getRoles = function() {
            $http.get(api.getRolesUrl, {}).success(function (resp) {
                $scope.roles = resp.data;
                if (roleId && roleId > 1) {
                    $scope.getRoleById(roleId);
                }
            });
        };

        var roleId = GetQueryString('roleId');
        if (typeof roleId == undefined || roleId <= 0) {
            roleId = 0;
        }

        if (roleId > 0) {
            $scope.pagetype = 'edit';
        }

        $scope.getRoles();

        $scope.save = function (){
            if (!$scope.roleName || $scope.saveRoles.length == 0) {
                Modal.error('有必填项没有填写');
                return false;
            }

            var url = roleId > 0 ? api.editRoleUrl : api.saveRoleUrl;
            $http.post(url, {
                "roleId": roleId,
                "name": $scope.roleName,
                'role': $scope.saveRoles
            }).success(function (resp) {
                if (resp.state) {
                    if (roleId > 0) {
                        Modal.alert('编辑成功');
                    } else {
                        Modal.alert({
                            title: "创建成功",
                            buttons: [{
                                name: "返回列表",
                                event: function () {
                                    location.href = "/admin/role/index"
                                }
                            }, {
                                name: "继续创建",
                                event: function () {
                                    $scope.roleName = "";
                                    $scope.saveRoles = [];
                                    angular.forEach($scope.roles, function(val) {
                                        $('#role-' + val.id).removeClass("fa-check-square").addClass("fa-square-o");
                                        if (val.subMenu.length > 0) {
                                            angular.forEach(val.subMenu, function (v) {
                                                $('#item-' + v.id).removeClass("fa-check-square").addClass("fa-square-o");
                                            });
                                        }
                                    });
                                }
                            }]
                        });
                    }
                } else {
                    Modal.error(resp.error ? resp.error : "角色创建失败");
                }
            });
        };

        $scope.selectRole = function (roleId, level) {
            roleId = parseInt(roleId);
            if ($scope.saveRoles && $scope.saveRoles.indexOf(roleId) > -1) {
                var index = $scope.saveRoles.indexOf(roleId);
                $scope.saveRoles.splice(index, 1);
            } else {
                $scope.saveRoles.push(roleId);
            }

            var roleItem = null, subItem = null;
            angular.forEach($scope.roles, function (val) {
                if (level > 0) {
                    if (roleId == val.id) {
                        roleItem = val;
                    }
                } else {
                    angular.forEach(val.subMenu, function (v) {
                        if (roleId == v.id) {
                            roleItem = val;
                            subItem = v;
                        }
                    });
                }
            });


            if (!roleItem) {
                return
            }

            if (level > 0) {
                if ($scope.saveRoles.indexOf(roleId) > -1) {
                    $('#role-' + roleId).removeClass("fa-square-o").addClass("fa-check-square");
                    if (roleItem.subMenu.length > 0) {
                        angular.forEach(roleItem.subMenu, function (item) {
                            $scope.saveRoles.push(parseInt(item.id));
                            $('#item-' + item.id).removeClass("fa-square-o").addClass("fa-check-square");
                        });
                    }
                } else {
                    $('#role-' + roleId).removeClass("fa-check-square").addClass("fa-square-o");
                    if (roleItem.subMenu.length > 0) {
                        angular.forEach(roleItem.subMenu, function (item) {
                            var index2 = $scope.saveRoles.indexOf(item.id);
                            $scope.saveRoles.splice(index2, 1);
                            $('#item-' + item.id).removeClass("fa-check-square").addClass("fa-square-o");
                        });
                    }
                }
            } else {
                // 子级
                if ($scope.saveRoles.indexOf(roleId) == -1) {
                    $('#item-' + roleId).removeClass("fa-check-square").addClass("fa-square-o");
                    $('#role-' + subItem.pid).removeClass("fa-check-square").addClass("fa-square-o");
                } else {
                    /// 检查父级是不是全部在
                    var willChecked = true;
                    angular.forEach(roleItem.subMenu, function (val){
                        if ($scope.saveRoles.indexOf(val.id) == -1) {
                            willChecked = false;
                        }
                    });

                    if (willChecked) {
                        $('#role-' + subItem.pid).removeClass("fa-square-o").addClass("fa-check-square");
                    }
                    $('#item-' + roleId).removeClass("fa-square-o").addClass("fa-check-square");
                }
            }
        };

        $scope.getRoleById = function (roleId) {
            $http.get(api.getByIDUrl, {
                params: {
                    roleId: roleId
                }
            }).success(function (res) {
                if (res.state) {
                    $scope.roleName = res.data.name;
                    $scope.saveRoles = !res.data.value ? [] : res.data.value;
                    $scope.venter();
                } else {
                    Modal.error('角色信息拉取失败');
                }
            });
        };

        $scope.venter = function () {
            if ($scope.roles.length == 0) {
                return false;
            }

            angular.forEach($scope.roles, function (val) {
                if ($scope.saveRoles && $scope.saveRoles.length > 0 && $scope.saveRoles.indexOf(val.id) > -1) {
                    $('#role-' + val.id).removeClass("fa-square-o").addClass("fa-check-square");
                }

                if (val.subMenu.length > 0) {
                    angular.forEach(val.subMenu, function (v) {
                        if ($scope.saveRoles && $scope.saveRoles.indexOf(v.id) > -1) {
                            $('#item-' + v.id).removeClass("fa-square-o").addClass("fa-check-square");
                        }
                    });
                }
            });
        };

    }]);

})();